import GameConst from "./GameConst";


class GameUtil {
    constructor(){

    }
    
    __extends(obj, parent) {
        function r() {
            this.constructor = obj;
        }
        for (var n in parent)
            if (parent.hasOwnProperty(n))
                obj[n] = parent[n];
        r.prototype = parent.prototype;
        obj.prototype = new r;
    }

    //获取url里面的参数
    getQueryString(name,deft)
    {
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r==null){
            return deft;
        }
        return r[2];     
    }

    isLocal(){
        console.log(window.origin);
        return window.origin && window.origin.startsWith('file:');
    }

    getInt(e) {
        return Math.floor(Math.random() * 1e5) % e;
    }
    setReg(node, t, n) {
        node.pivotX = node.width * t;
        node.pivotY = node.height * n;
    }
    distanceBetweenPoints(x1, y1, x2, y2) {
        var a = x1-x2,b=y1-y2;
        return Math.sqrt(a * a + b * b);
    }
    removeClip(e) {
        if (e && e.parent)
            e.parent.removeChild(e);
    }
    cjp(e, t) {
        return new Laya.Point(e, t);
    }

    getPoint(x, y) {
        if (typeof x === "undefined") {
            x = 0;
        }
        if (typeof y === "undefined") {
            y = 0;
        }
        var testPoint = new Point
        testPoint.x = x;
        testPoint.y = y;
        return testPoint;
    }
    /**
     * 动画扭扭扭 ， 每一帧都是一个sprite
     * */
    createSpriteFromSpritesheet(name, t) {
        // if (typeof t === "undefined") {
        //     t = 0;
        // }
        // var anmi = new Laya.Animation();
        // anmi.loadAnimation(this.getResUrl(name));
        // anmi.name= name;
        // anmi.autoPlay=false;
        //return anmi;
        var img = new DynamicSprite();
        var res = this.getResUrl(name);
        if(typeof res === 'object'){
            for (const key in res) {
                img.addImage(key,new Laya.Image(res[key]));
            }
            return img;
        }
        img.gotoAndStop(name);
        return img;
    }

    createSprite(name){
        var img = new Laya.Sprite();
        // var image = new Laya.Image(name);
        // img.addChild(image);
        img.loadImage(name);
        img.name = name;
        return img;
    }
    
    addChild(e, t) {
        t.addChild(e);
    }
    createBitmap(skin) {
        var t = new Laya.Image(this.getResUrl(skin));
        t.name  = skin;
        t.gotoAndStop = function(){}
        //t.play = function(){}
        return t;
    }
    limitDt(e) {
        return e;
    }
    limit(num, min, max) {
        if (num < min)
            num = min;
        if (num > max)
            num = max;
        return num;
    }
    /*
    * 获取在A,B 两数之间的一个值,P {0-1}是取值的范围,p=0=,返回a,p=1,返回b 
    * @param Number a
    * @param {Number} b
    * @param {Number} p
    * @returns {Number}
    */
    lerp(a, b, p) {
        return a + p * (b - a);
    }
    /*
    * 获取在A,B 两角度之间的一个值
    * @param {type} a
    * @param {type} b
    * @param {type} rate 取值的范围
    * @returns {Number}
    */
    lerpAngle(a, b, rate) {
        var r = Math.abs(b - a);
        if (r > 180) {
            if (b > a)
                a += 360;
            else
                b += 360;
        }
        var i = a + (b - a) * rate;
        return i%360;
    }
    sign(e) {
        return e > 0 ? 1 : -1;
    }
    rotatePoint(point, ang) {
        ang *= Math.PI / 180;
        var n = Math.sin(ang);
        var r = Math.cos(ang);
        var x = point.x * r - point.y * n;
        var y = point.x * n + point.y * r;
        point.x = x;
        point.y = y;
    }

    trace(data){
        var uid = GameConst.adapter.getUid();
        var http = new Laya.HttpRequest();
        data['uid']=uid;
        http.send("https://gametrace.18yb.com/jelly",data,"post","text");
    }
    /***
     * 创建动画
     * type : 1001,1002 ....
     * from-to
     * speed
     * next 
     * stoptimes  应该是最后一贞的重复次数
     */
    createAnimation(name, type, from, to, speed, next, stoptimes) {
        // if (typeof speed === "undefined") {
        //     speed = 1;
        // }
        // if (typeof next === "undefined") {
        //     next = null;
        // }
        // if (typeof stoptimes === "undefined") {
        //     stoptimes = 0;
        // }
        // var frames = [];
        // var forward = from <= to;
        // for (var idx = from; forward && idx <= to || !forward && idx >= to; forward ? ++idx : --idx) {
        //     var key = type + (idx <= 9 ? "000" : "00") + idx;
        //     var arr = spriteSheetInfo.animations[key];
        //     var frame = arr[0];
        //     if (frame == undefined || frame == null || isNaN(frame))
        //         console.log("ERROR"+key);
        //     var c = 1 + (idx == to ? stoptimes : 0);
        //     for (var h = 0; h < c; ++h)
        //         frames.push(frame);
        // }
        // spriteSheetInfo.animations[name] = {
        //     frames: frames,
        //     next: next ? next : name,
        //     speed: speed
        // };
        var anm = new Laya.Animation();
        anm.loadAnimation(this.getResUrl(name));
        anm.loop = false;
        anm.name = name;
        anm.stop();
        // this.spriteSheetInfo[name] = amni;
        return anm;
    }
    createFontFrames(e) {
        var charStr = "abcdefghijklmnopqrstuvwxyz1234567890!-./:%";
        for (var i = 0; i < charStr.length; ++i) {
            var r = charStr.charCodeAt(i);
            var char = charStr.charAt(i);
            if (e.animations[r.toString()])
                e.animations[char] = e.animations[char.toUpperCase()] = e.animations[r.toString()];
        }
    }
    getTextSize(text) {
        // var text = new Laya.Text();
        // var t = text.text.length;
        // var n = text.letterSpacing;
        var r = text.width;
        var i = text.height;
        // for (var s = 0; s < t; ++s) {
        //     var o = text.text.charAt(s);
        //     var u = text._getFrame(o, text.spriteSheet);
        //     if (o == " ")
        //         r += text.spaceWidth;
        //     else {
        //         i = Math.max(i, u.rect.height);
        //         r += u.rect.width + (s == 0 ? 0 : n);
        //     }
        // }
        var textSize  = this.cjp(r,i);
        return textSize;
    }
    traceChildren(e, t) {
        if (typeof t === "undefined") {
            t = 0;
        }
        var n = e.getBounds();
        var r = e instanceof createjs.Container ? e : null;
        console.log(t, e.x, e.y, n ? n.width : "null", n ? n.height : "null", r != null);
        if (Math.abs(e.x) > 2e3 || Math.abs(e.y) > 2e3)
            console.log("error!");
        if (r) {
            for (var i = 0; i < r.getNumChildren(); ++i)
                traceChildren(r.getChildAt(i), t + 1);
        }
    }
    touchHandlerDummy(e) {
        try{
            e.preventDefault();
        }catch(ex){
        }
        return false;
    }

    /**
     * 是否竖屏被锁定
     *  */
    isOrientationLocked() {
        return false;
        if (!viewporter || !viewporter.ACTIVE) {
            return false;
        }
        var e = window.innerWidth > window.innerHeight && window.innerWidth <= 640;
        return e || viewporter.isLandscape();
    }

    getCellDataType(str, n, r) {
        if (!str)
            return "";
        return str.charAt(2 * (r + n * GameConst.LEVEL_H) + 1)
    };


    gridToStageX(x) {
        return GameConst.OFFSET_X + (x + .5) * GameConst.TILE_SIZE
    };
    gridToStageY(y) {
        return GameConst.OFFSET_Y + (y + .5) * GameConst.TILE_SIZE
    };
    stageToGridX(x) {
        return (x - GameConst.OFFSET_X) / GameConst.TILE_SIZE
    };
    stageToGridY(y) {
        return (y - GameConst.OFFSET_Y) / GameConst.TILE_SIZE
    };
    getCellCenterCoordinatesX(x) {
        return GameConst.OFFSET_X + (x + .5) * GameConst.TILE_SIZE
    };
    getCellCenterCoordinatesY(y) {
        return GameConst.OFFSET_Y + (y + .5) * GameConst.TILE_SIZE
    }

    getResUrl(name){
        var url = GameUtil.resDic[name];
        if(!url){
            console.log("unkown name:"+name);
        }
        return url?url:name;
    }
}
GameUtil.resDic = {
    'cookie':'textures/jelly/cookie.png',
    'cookie honey':'textures/jelly/cookie honey.png',
    'cookie x3':'textures/jelly/cookie x3.png',
    'cookie x2':'textures/jelly/cookie x2.png',    
    'pause_button':'textures/jelly/pause button.png',
    'progressbar_star':'textures/jelly/progressbar star.png',
    'gray':'textures/jelly/gray.png',
    'menu_back':'ui/menu back.jpg',
    'black':'game/back.png',

    'bush root':'textures/jelly/bush root.png',
    'star':'game/star.png',
    'bonus_line_side':'textures/jelly/bonus line side.png',
    'bonus_line_center':'textures/jelly/bonus line center.png',
    'bonus_new':'textures/jelly/bonus new.png',
    'explosion_bomb':'textures/jelly/explosion bomb.png',
    'explosion_line':'textures/jelly/explosion line.png',
    'star_glow':'textures/jelly/star glow.png',
    'cupcake':'textures/jelly/cupcake.png',
    'bush':'textures/jelly/bush.png',
    'water':'textures/jelly/water.png',
    'mine_sprite_x':'textures/jelly/mine_sprite_x.png',
    'wall color':'textures/jelly/wall color.png',
    'chocolate nuts':'textures/jelly/chocolate nuts.png',
    'chocolate white':'textures/jelly/chocolate white.png',
    'chocolate black':'textures/jelly/chocolate black.png',
    'chocolate black2':'textures/jelly/chocolate black2.png',
    'chocolate black x3 full':'textures/jelly/chocolate black x3 full.png',
    'menu_transfer':'menu transfer.png',
    'pointerTween 110_t':'textures/jelly/pointerTween 110_t.png',
    'point_big_pinc':'textures/jelly/point big pinc.png',
    'point_big_blue':'textures/jelly/point big blue.png',
    'point_small_pinc':'textures/jelly/point small pinc.png',
    'point_small_blue':'textures/jelly/point small blue.png',
    'Victory':'textures/jelly/Victory.png',
    'lose':'textures/jelly/lose.png',
    'star_back':'textures/jelly/star back.png',
    'buttonTween 20_t':'textures/jelly/buttonTween 20_t.png',
    'buttons pause animbuttons pause5_t':'textures/jelly/buttons pause animbuttons pause5_t.png',
    'buttons pause animbuttons pause6_t':'textures/jelly/buttons pause animbuttons pause6_t.png',
    'buttons pause animbuttons pause3_t':'textures/jelly/buttons pause animbuttons pause3_t.png',
    'buttons pause animbuttons pause2_t':'textures/jelly/buttons pause animbuttons pause2_t.png',
    'buttons pause animbuttons pause1_t':'textures/jelly/buttons pause animbuttons pause1_t.png',
    'buttons pause animbuttons pause0_t':'textures/jelly/buttons pause animbuttons pause0_t.png',
    
    
    'blue':{blue:'textures/jelly/blue.png',
        'blue vertical':'textures/jelly/blue vertical.png',
        'blue horizontal':'textures/jelly/blue horizontal.png',
        'blue bomb':'textures/jelly/blue bomb.png',
        'bonus_color':'textures/jelly/bonus color.png',
        'blue bonus':'textures/jelly/blue bonus.png'},
    'blue vertical':'textures/jelly/blue vertical.png',
    'blue horizontal':'textures/jelly/blue horizontal.png',
    'blue bomb':'textures/jelly/blue bomb.png',
    'blue bonus':'textures/jelly/blue bonus.png',
    

    'orange':{orange:'textures/jelly/orange.png',
        'orange vertical':'textures/jelly/orange vertical.png',
        'orange horizontal':'textures/jelly/orange horizontal.png',
        'bonus_color':'textures/jelly/bonus color.png',
        'orange bomb':'textures/jelly/orange bomb.png',
        'orange bonus':'textures/jelly/orange bonus.png'},
    'orange vertical':'textures/jelly/orange vertical.png',
    'orange horizontal':'textures/jelly/orange horizontal.png',
    'orange bomb':'textures/jelly/orange bomb.png',
    'orange bonus':'textures/jelly/orange bonus.png',

    'green':{green:'textures/jelly/green.png',
        'green vertical':'textures/jelly/green vertical.png',
        'green horizontal':'textures/jelly/green horizontal.png',
        'bonus_color':'textures/jelly/bonus color.png',
        'green bomb':'textures/jelly/green bomb.png',
        'green bonus':'textures/jelly/green bonus.png'},
    'green vertical':'textures/jelly/green vertical.png',
    'green horizontal':'textures/jelly/green horizontal.png',
    'green bomb':'textures/jelly/green bomb.png',
    'green bonus':'textures/jelly/green bonus.png',

    'purple':{purple:'textures/jelly/purple.png',
        'purple vertical':'textures/jelly/purple vertical.png',
        'purple horizontal':'textures/jelly/purple horizontal.png',
        'purple bomb':'textures/jelly/purple bomb.png',
        'bonus_color':'textures/jelly/bonus color.png',
        'purple bonus':'textures/jelly/purple bonus.png'},


    'purple vertical':'textures/jelly/purple vertical.png',
    'purple horizontal':'textures/jelly/purple horizontal.png',
    'purple bomb':'textures/jelly/purple bomb.png',
    'purple bonus':'textures/jelly/purple bonus.png',
    
    'bluex':'ani/blue.ani',
    'orangex':'ani/orange.ani',
    'greenx':'ani/green.ani',
    'purplex':'ani/purple.ani',
    'cupcakex':'ani/cupcake.ani',
    'bonus_blink':'ani/bonus_blink.ani',
    'color_bonus_anm':'ani/color_bonus.ani',
    'teleport_in':'ani/teleport_in.ani',
    'teleport_out':'ani/teleport_out.ani',
    'bushx':'ani/bush.ani',
    'chocolate':'ani/chocolate.ani',
    'chocolatewhite':'ani/chocolatewhite.ani',
    'water_splash':'ani/water_splash.ani',
    'color_wall':'ani/color_wall.ani',
    'bush_anim':'ani/bush.ani',   
    

    'unbelievable':'game/tip_unbelievable.png',
    'combo':'game/tip_good.png',
    'amazing':'game/tip_amazing.png',
    
    'logo_down':'ui/logo down.jpg',
    'logo_top':'ui/logo top.jpg',
    'bar':'ui/bar.png',

    
    
    '':false
};
var Utils = new GameUtil;
export default Utils;

export class DynamicSprite extends Laya.Sprite{
    constructor(){
        super();
        this.img = null;
        this.images = {};
    }

    

    gotoAndStop(name){
        var img = this.images[name];
        if(!img){
            img = Utils.createBitmap(name);
            this.addImage(name,img);
        }
        if(img){
            img.name = name;
            if(this.img){
                this.removeChild(this.img);
            }
            this.img = img;
            img.pivotX = img.width/2;
            img.pivotY = img.height/2;
            this.addChild(img);
        }else{
        }
            
    }

    addImage(name,image){
        this.images[name] = image;
    }

    gotoAndPlay(aniName){
        if(this.img){
            this.removeChild(this.img);
        }
        var anim = Utils.createAnimation(aniName);
        this.addChild(anim);
        anim.play(0,false);
        anim.on(Laya.Event.COMPLETE,this,this.onPlayEnd);
        this.img = anim;
    }

    onPlayEnd(){
        this.removeChild(this.img);
        this.img = null;
    }
}